public with sharing class Cald_DetailPayoutVF {
    private final Callidus_Plan__c[] plan = new Callidus_Plan__c[1];
    public Callidus_Plan__c[] planObjList {get; set;}
    public List<Callidus_Rate__c> ratesList {get; set;}
    private List<String> drawPointsList = new List<String>();
    private List<String> yAxisList = new List<String>();
    private List<String> yAxisValueList = new List<String>();
    private String[] xAxisList = new String[]{'0', '20', '40', '60', '80', '100', '120', '140', '160', '180', '200', '220'};
    
    private String initialChartURL = 'http://chart.apis.google.com/chart?';
    private Double maxRateForPlan = 0;
    public String url {get; set;}
    public String rateType {get; set;}
    public Boolean displayRateTable {get; set;}
    public Cald_DetailPayoutVF(ApexPages.StandardController myController)
    {
        try
        {
            plan[0] = (Callidus_Plan__c)myController.getRecord();
            planObjList = [select id, Quota__c, OTE__c from Callidus_Plan__c where id = :plan[0].id];
            ratesList = [select Min_Attainment__c, Max_Attainment__c, Rate__c, Rate_Type__c from Callidus_Rate__c where plan__c = :plan[0].id];
            for(Integer i=(ratesList.size() - 1); i>=0; i--)
            {
                maxRateForPlan = ratesList[i].Max_Attainment__c;
                rateType = ratesList[i].Rate_Type__c;
                break;
            }
            displayRateTable = true;
            if(rateType == 'Credit' || ratesList.size() == 0)
                displayRateTable = !displayRateTable;
            
            //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, ''+maxRateForPlan));
            for(Integer i=0; i<=200; (i=i+20))
                drawPointsList.add(String.valueOf(i));
            //ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, ''+drawPointsList));
            
            Double val = 0;
            Double initPercent = 0;
            for(Integer y=0; y <=6; y++)
            {
                val = (initPercent * planObjList[0].Quota__c)/100;
                initPercent += 2.5;
                yAxisList.add(String.valueOf(val.round()));
            } 
            
            getAttainmentTable('lxy');
        }
        catch(Exception e)
        {
//          ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, e.getMessage()));
        }
    }
    
    private String getAttainmentTable(String chartType)
    {
        if(chartType == 'lxy')
        {
            initialChartURL += 'cht=lxy&chd=t:';
            Integer i = 0;
            String lastAttainmentVal = '';
            for(String s: drawPointsList)
            {
                if(i == 0)
                {
                    initialChartURL += s;
                    i++;
                }
                else
                    initialChartURL += ','+s;
                lastAttainmentVal = s;  
            }
            initialChartURL += '|';
            i = 0;
            Double val = 0;
            Double attainmentUSD = 0;
            for(String s: drawPointsList)
            {
            	attainmentUSD = 0;
                for(Callidus_Rate__c r: ratesList)
                {
                    if(Double.valueOf(s) > 0 && r.min_Attainment__c < Double.valueOf(s) && r.max_Attainment__c >= Double.valueOf(s))
                    {
                        attainmentUSD = (Double.valueOf(s) * planObjList[0].Quota__c)/100;
                        
                        s = String.valueOf(r.Rate__c);
                        break;
                    }
                }
                val = (Double.valueOf(s) * attainmentUSD)/100;
                yAxisValueList.add(String.valueOf(val.round()));
                if(i == 0)
                {
                    initialChartURL += '0';
                    i++;
                }
                else
                    initialChartURL +=','+ String.valueOf(val.round());
            }
            //initialChartURL += '&chds=0,'+lastAttainmentVal+',0,'+yAxisList[yAxisList.size() - 1]+'&chs=370x240&chtt=Payout+by+Attainment&chxt=x,y,x,y&chxl=0:|0|20|40|60|80|100|120|140|160|180|200|1:|';
            
            initialChartURL += '&chds=0,200,0,'+yAxisValueList[yAxisValueList.size() - 1]+'&chs=370x240&chtt=Payout+by+Attainment&chxt=x,y,x,y&chxl=0:|0|20|40|60|80|100|120|140|160|180|200|1:|';
        	//ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.info, initialChartURL));
            i = 0;
            for(String s : yAxisList)
            {
                if(i == 0)
                {
                    initialChartURL += s;
                    i++;
                }
                else
                    initialChartURL += '|$'+s;
                
            }
            
            initialChartURL += '|2:|Attainment%20(%)|3:|Payout&chxp=2,50|3,50&chco=000000&chf=bg,s,F3F3EC&chm=d,246CBE,0,-1,8,0|N*cUSD1*,246CBE,0,-1,9&chg=10,10,1,5';
        }
        url = initialChartURL;

        return null;
    }
    
    static testMethod void testCald_DetailPayoutVF()
    {
        
        PageReference pg = Page.Cald_Detail_Payout;
        Test.setCurrentPage(pg);
        
        Callidus_Plan__c plan = new Callidus_Plan__c(Name = 'Test', Start_Date__c = Date.today(), End_Date__c = (Date.today() + 100), Quota__c = 1000000);
        insert plan;
        
        Callidus_Rate__c rate = new Callidus_Rate__c(Plan__c = plan.id, Min_Attainment__c = 0, Max_attainment__c = 100, Rate__c = 1);
        insert rate;
        rate = new Callidus_Rate__c(Plan__c = plan.id, Min_Attainment__c = 100, Max_attainment__c = 110, Rate__c = 2);
        insert rate;
        ApexPages.StandardController mc = new Apexpages.Standardcontroller(plan);
        Cald_DetailPayoutVF dp = new Cald_DetailPayoutVF(mc);
        system.assertEquals(null, dp.getAttainmentTable('lxy'));
    }
}